﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;


namespace coffeeshop.Forms
{
    public partial class frmUnitList : Form
    {
        private bool formLoaded = false;
        //private SMK_EditListView listUnit;
        public frmUnitList()
        {
            InitializeComponent();
            LoadUnits(true);
            setButtonEditEnable();
        }

        private void frmUnitList_Shown(object sender, EventArgs e)
        {
            formLoaded = true;
        }

        private void setButtonEditEnable()
        {
            btnEdit.Enabled = listUnit.SelectedItems.Count > 0;
        }

        public void ReloadList()
        {
            LoadUnits(ckboxHideInactive.Checked);
            setButtonEditEnable();
        }

        private void LoadUnits(bool toHideInactive)
        {
            listUnit.Clear();
            listUnit.ItemCheck += new ItemCheckEventHandler(listUnit_ItemCheck);
            //listUnit.MouseDoubleClick += new MouseEventHandler(listUser_ItemDoubleClick);
            listUnit.View = View.Details;
            // Display check boxes.
            listUnit.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listUnit.FullRowSelect = true;
            // Display grid lines.
            listUnit.GridLines = true;

            coffeeDataSet.UnitDataTable unitList = Unit.LoadUnits(toHideInactive);
            listUnit.Columns.Add("Kích hoạt", 70, HorizontalAlignment.Left);
            listUnit.Columns.Add("Tên", 80, HorizontalAlignment.Left);
            listUnit.Columns.Add("UnitId", 0, HorizontalAlignment.Left);

            foreach (DataRow row in unitList.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Checked = (bool)row["IsActive"];
                item.SubItems.Add(row["Name"].ToString());
                item.SubItems.Add(row["UnitId"].ToString());

                listUnit.Items.Add(item);
            }
        }

        private void ckboxHideInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (!formLoaded)
                return;

            bool isChecked = ((CheckBox)sender).Checked;

            LoadUnits(isChecked);
        }

        private void listUnit_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
        {
            if (!formLoaded)
                return;

            bool isActive = e.CurrentValue == CheckState.Unchecked;

            Unit theObject = new Unit(int.Parse(listUnit.Items[e.Index].SubItems[2].Text));
            theObject.IsActive = isActive;

            if (!theObject.UpdateActiveness())
            {
                MessageBox.Show("Có lỗi khi thay đổi kích hoạt");
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmEditUnit editForm = new frmEditUnit(null);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listUnit.SelectedItems.Count == 0)
            {
                MessageBox.Show("Hãy chọn một dòng trước");
                return;
            }
            ListViewItem selectedItem = listUnit.SelectedItems[0];
            Unit _unit = new Unit(int.Parse(selectedItem.SubItems[2].Text));
            _unit.Name = selectedItem.SubItems[1].Text;

            frmEditUnit editForm = new frmEditUnit(_unit);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void listUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            setButtonEditEnable();
        }
    }
}
